package com.ricervcvcer.category.controller;


import com.ricervcvcer.category.client.BrandClient;
import com.ricervcvcer.category.entity.TbBrand;
import com.ricervcvcer.category.entity.TbCategory;
import com.ricervcvcer.category.service.ITbCategoryService;
import com.ricervcvcer.category.vo.CategoryBrandVo;
import com.ricervcvcer.category.vo.CategoryVO;
import com.ricervcvcer.common.util.ResultEntity;
import com.ricervcvcer.common.util.RicervcvcerConstant;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品类目表，类目和商品(spu)是一对多关系，类目与品牌是多对多关系 前端控制器
 * </p>
 *
 * @author moge
 * @since 2022-01-05
 */
@Slf4j
@RestController
@RequestMapping("category")
public class TbCategoryController {
    @Autowired
    private ITbCategoryService categoryService;

    @Autowired
    private BrandClient brandClient;

    /*
    * 查询某一个分类与分类下的所有品牌
    * */
    @ApiOperation("根据主键查询分类对象后查询对应品牌对象")
    @GetMapping("id/{cid}")
    public ResponseEntity<CategoryBrandVo> getCategoryById(
            @ApiParam("分类主键") @PathVariable Long cid
    ){
        TbCategory category = categoryService.getById(cid);
        log.debug("根据分类主键{}查询分类对象", cid);
        CategoryBrandVo categoryBrandVo = new CategoryBrandVo();
        BeanUtils.copyProperties(category, categoryBrandVo);
        List<Long> brandIds = categoryService.selectBrandIdsByCategoryId(cid);
        log.debug("根据分类主键{}查询品牌对象主键集合", cid);
        ResponseEntity<List<TbBrand>> brands = brandClient.getBrandsByIds(brandIds);
        categoryBrandVo.setBrands(brands.getBody());
        return ResponseEntity.ok(categoryBrandVo);
    }

    @ApiOperation("查询所有品牌")
    @GetMapping("getAll")
    public ResultEntity getAll(){
       List<CategoryVO> categorys = categoryService.brands();
        log.debug("递归查询所有分类");
        return ResultEntity.successWithData(categorys);
    }

    @ApiOperation("根据分类id查询")
    @GetMapping("byId/{id}")
    public ResultEntity<TbCategory> getById(@PathVariable("id") Long id) {
         TbCategory category = categoryService.getById(id);
         return ResultEntity.successWithData(category);
    }

    @ApiOperation("添加分类")
    @PostMapping("add")
    public ResultEntity<TbCategory> add(@RequestBody TbCategory category) {
        boolean  b = categoryService.save(category);
        if(!b){
            throw new RuntimeException(RicervcvcerConstant.ADD_ERROR);
        }
        return ResultEntity.successWithData(category);
    }

    @ApiOperation("修改分类")
    @PostMapping("update")
    public ResultEntity update(@RequestBody TbCategory category) {
        boolean  b = categoryService.updateById(category);
        if(!b){
            throw new RuntimeException(RicervcvcerConstant.UPDATE_ERROR);
        }
        return ResultEntity.successWithoutData();
    }
}
